﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sanchay
{
    public partial class PurchaseHistory : Form
    {
        public PurchaseHistory()
        {
            InitializeComponent();
        }

        private void PurchaseHistory_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = Convert.ToDateTime("01-Apr-" + (DateTime.Today.Year));
            dtpTo.Value = Convert.ToDateTime("31-Mar-" + (DateTime.Today.Year + 1));
            SearchInvoiceList();

        }

        private void BindInvoiceListGrid(DataTable dt)
        {
            dgvPurchaseInvoiceList.DataSource = dt;

            dgvPurchaseInvoiceList.Columns[0].DataPropertyName = "Date";
            dgvPurchaseInvoiceList.Columns[1].DataPropertyName = "Invoice No.";
            dgvPurchaseInvoiceList.Columns[2].DataPropertyName = "Amount";
            dgvPurchaseInvoiceList.Columns[3].DataPropertyName = "Supplier";

         
            dgvPurchaseInvoiceList.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
            dgvPurchaseInvoiceList.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
            dgvPurchaseInvoiceList.Columns[0].FillWeight = 15;
            dgvPurchaseInvoiceList.Columns[1].FillWeight = 30;
            dgvPurchaseInvoiceList.Columns[2].FillWeight = 15;
            dgvPurchaseInvoiceList.Columns[3].FillWeight = 40;
        }

      
        private void dgvPurchaseInvoiceList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SparePurchaseRegister oSparePurchaseRegister = new SparePurchaseRegister(true, dgvPurchaseInvoiceList.CurrentRow.Cells[1].Value.ToString());
                oSparePurchaseRegister.MdiParent = DashBoard.ActiveForm;
                oSparePurchaseRegister.Show();
            }
        }

        private void txtPartsNo_TextChanged(object sender, EventArgs e)
        {
            SearchInvoiceList();
        }

      
        private void txtSupplier_TextChanged(object sender, EventArgs e)
        {
           SearchInvoiceList();
            
        }

        private void SearchInvoiceList()
        {
            DataAccess da = new DataAccess();
            string query = "SELECT CONVERT(varchar, Date, 106) AS Date, [Invoice No.], Amount, Supplier FROM  vw_PurchaseInvoiceList " +
                "WHERE [Invoice No.] LIKE '%" + txtInvoiceNo.Text + "%'" +
               " And [Supplier] LIKE '%" + txtSupplier.Text + "%'" +
               " AND Date BETWEEN '" + dtpFrom.Value.ToString("MM/dd/yyyy") + "' AND '" + dtpTo.Value.ToString("MM/dd/yyyy") + "'";
               //" AND (Date >= '" + dtpFrom.Value.ToShortDateString() + " 12:00:00AM') AND (Date <= '" + dtpTo.Value.ToShortDateString() + " 11:59:59PM')";
               
            DataTable dt = da.SelectCommand(query);
            if (dt != null)
            {
                BindInvoiceListGrid(dt);
                lblRecordCount.Text = " Total Record(s) Found: " + dt.Rows.Count.ToString();
            }
        }

        private void dtpFrom_ValueChanged(object sender, EventArgs e)
        {
            SearchInvoiceList();
        }

        private void dtpTo_ValueChanged(object sender, EventArgs e)
        {
            SearchInvoiceList();
        }

        private void dgvPurchaseInvoiceList_DoubleClick(object sender, EventArgs e)
        {
            SparePurchaseRegister oSparePurchaseRegister = new SparePurchaseRegister(true, dgvPurchaseInvoiceList.CurrentRow.Cells[1].Value.ToString());
            oSparePurchaseRegister.MdiParent = DashBoard.ActiveForm;
            oSparePurchaseRegister.Show();
        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    DataAccess da = new DataAccess();
        //    string query = "select partsno from inventorymaster group by partsno";
        //    string query1,query2 = "";
        //    string partsNo = "";
        //    DataTable dt = da.SelectCommand(query);
        //    DataTable dt1;
        //    int inventoryid;
        //    int totalInventory = dt.Rows.Count;
        //    for (int i = 0; i < totalInventory; i++)
        //    {
        //        Application.DoEvents();
        //        partsNo = dt.Rows[i][0].ToString();
        //        query1 = "select * from inventorymaster where partsno='" + partsNo + "' order by inventoryid asc";
        //        dt1 = da.SelectCommand(query1);

        //        for (int j = 0; j < dt1.Rows.Count; j++)
        //        {
        //            inventoryid = (int)dt1.Rows[j][0];
        //            switch ((int)dt1.Rows[j][6])
        //            {
        //                case 1:// Purchase
        //                    if (j == 0)
        //                        query2 = "update inventorymaster set availableqty=" + (int)dt1.Rows[j][2] + " where inventoryid=" + inventoryid;
        //                    else
        //                        query2 = "update inventorymaster set availableqty=" + ((int)dt1.Rows[j][2] + (int)dt1.Rows[j - 1][3]) + " where inventoryid=" + inventoryid;
        //                    break;
        //                case 2:// Sales
        //                   if (j == 0)
        //                       query2 = "update inventorymaster set availableqty=" + (int)dt1.Rows[j][2] + " where inventoryid=" + inventoryid;
        //                    else
        //                       query2 = "update inventorymaster set availableqty=" + ((int)dt1.Rows[j - 1][3] - (int)dt1.Rows[j][2]) + " where inventoryid=" + inventoryid;
        //                    break;
        //                case 5:// PurchaseItemDelete
        //                    if (j == 0)
        //                        query2 = "update inventorymaster set availableqty=" + (int)dt1.Rows[j][2] + " where inventoryid=" + inventoryid;
        //                    else
        //                        query2 = "update inventorymaster set availableqty=" + ((int)dt1.Rows[j - 1][3] - (int)dt1.Rows[j][2]) + " where inventoryid=" + inventoryid;
        //                    break;
        //                case 7:// SalesItemDeletes
        //                   if (j == 0)
        //                       query2 = "update inventorymaster set availableqty=" + (int)dt1.Rows[j][2] + " where inventoryid=" + inventoryid;
        //                    else
        //                       query2 = "update inventorymaster set availableqty=" + ((int)dt1.Rows[j][2] + (int)dt1.Rows[j - 1][3]) + " where inventoryid=" + inventoryid;
        //                    break;
        //            }

        //            da.DoCommand(query2);
        //            if (j != dt1.Rows.Count - 1)
        //                da.DoCommand("update inventorymaster set iscurrent=0 where inventoryid=" + inventoryid);
        //            else
        //                da.DoCommand("update inventorymaster set iscurrent=1 where inventoryid=" + inventoryid);
        //        }

        //        progressBar1.Value = i / (totalInventory-1) * 100;
        //        label1.Text = i.ToString();
        //    }

        //}
    }
}
